import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/components/layout/index.vue'
Vue.use(VueRouter)

// 所有权限通用路由表
// 如首页和登录页和一些不用权限的公用页面
export const constantRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    hidden: true,
    meta: {
      title: '首页',
      icon: 'el-icon-eleme',
      breadcrumb: false
    },
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import(/* webpackChunkName:'Home' */ '@/views/Home/index.vue'),
        meta: {
          title: '首页',
          icon: 'el-icon-eleme',
          breadcrumb: false
        }
      }
    ]
  },
  {
    path: '/login',
    hidden: true,
    component: () => import(/* webpackChunkName:'Login' */ '@/views/Login/index.vue'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/404',
    hidden: true,
    component: () => import(/* webpackChunkName:'Error' */ '@/views/Error/404.vue'),
    meta: {
      title: '404'
    }
  }
]

const createRouter = () => new VueRouter({
  mode: 'history',
  routes: constantRoutes
})

const router = createRouter()
// 异步挂载的路由
// 动态需要根据权限加载的路由表

export const asyncRoutes = [
  {
    path: '/admin',
    component: Layout,
    redirect: '/admin/A',
    meta: {
      title: '管理',
      icon: 'el-icon-tickets',
      roles: ['admin']
    },
    children: [
      {
        path: '/admin/A',
        component: () => import('@/views/A/index.vue'),
        name: 'A', // 设定路由的名字, 用于params路由传参时的参数
        hidden: false, // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1 (默认 false)
        meta: {
          roles: ['admin', 'editor'], // 设置该路由进入的权限，支持多个权限叠加
          title: 'title', // 设置该路由在侧边栏和面包屑中展示的名字
          icon: 'el-icon-tickets', // 设置该路由的图标, 支持element-ui内置icon
          noCache: true // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
        }
      },
      {
        path: '/admin/B',
        component: () => import('@/views/B/index.vue'),
        name: 'B',
        meta: {
          title: 'B',
          icon: 'el-icon-tickets',
          roles: ['admin']
        }
      },
      {
        path: '/admin/C',
        component: () => import('@/views/C/index.vue'),
        name: 'C',
        meta: {
          title: 'C',
          icon: 'el-icon-tickets',
          roles: ['admin']
        }
      }
    ]
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true,
    meta: {
      title: '404'
    }
  }
]

export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
